package com.sooth.micro.camunda.controller;

import com.dream.system.config.Page;
import com.sooth.common.core.domain.R;
import com.sooth.common.dream.model.PageModel;
import com.sooth.common.web.core.BaseController;
import com.sooth.micro.camunda.converter.bean.ProcessModel;
import com.sooth.micro.camunda.service.IMicroCamundaService;
import com.sooth.micro.camunda.view.*;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/micro/camunda")
public class MicroCamundaController extends BaseController {

    private final IMicroCamundaService camundaService;

    @PostMapping(value = "/deploy")
    public R<Page> deploy(@RequestBody DeployVo deployVo) {
        camundaService.deploy(deployVo);
        return R.ok();
    }

    @PostMapping(value = "/start")
    public R<Page> start(@RequestBody ProcessVo processVo) {
        camundaService.start(processVo);
        return R.ok();
    }

    @PostMapping(value = "/approve")
    public R<Page> approve(@RequestBody ApproveVo approveVo) {
        camundaService.approve(approveVo);
        return R.ok();
    }

    /**
     * 转办审批人
     *
     * @param transferVo
     * @return
     */
    @PostMapping(value = "/transfer")
    public R<Page> transfer(@RequestBody TransferVo transferVo) {
        camundaService.transfer(transferVo);
        return R.ok();
    }

    /**
     * 委派审批人
     *
     * @param delegateVo
     * @return
     */
    @PostMapping(value = "/delegate")
    public R<Page> transfer(@RequestBody DelegateVo delegateVo) {
        camundaService.delegate(delegateVo);
        return R.ok();
    }

    @GetMapping(value = "/reject")
    public R<ProcessModel> reject(@RequestParam String taskId) {
        camundaService.reject(taskId);
        return R.ok();
    }

    @GetMapping(value = "/convertToModelByProcessDefinitionId")
    public R<ProcessModel> convertToModelByProcessDefinitionId(@RequestParam String processDefinitionId) {
        return R.ok(camundaService.convertToModelByProcessDefinitionId(processDefinitionId));
    }

    @GetMapping(value = "/convertToModelByProcessInstanceId")
    public R<ProcessModel> convertToModelByProcessInstanceId(@RequestParam String processInstanceId) {
        return R.ok(camundaService.convertToModelByProcessInstanceId(processInstanceId));
    }

    @GetMapping(value = "/convertToModelByTaskId")
    public R<ProcessModel> convertToModelByTaskId(@RequestParam String taskId) {
        return R.ok(camundaService.convertToModelByTaskId(taskId));
    }

    @GetMapping(value = "/suspendProcessDefinitionById")
    public R<Void> suspendProcessDefinitionById(@RequestParam String processDefinitionId) {
        camundaService.suspendProcessDefinitionById(processDefinitionId);
        return R.ok();
    }

    @GetMapping(value = "/activateProcessDefinitionById")
    public R<Void> activateProcessDefinitionById(@RequestParam String processDefinitionId) {
        camundaService.activateProcessDefinitionById(processDefinitionId);
        return R.ok();
    }

    @GetMapping(value = "/deploymentList")
    public R<Page> deploymentList(ProcessDefinitionDto processDefinitionDto, PageModel page) {
        return R.ok(camundaService.deploymentList(processDefinitionDto, page));
    }

    @DeleteMapping(value = "/deleteDeployment")
    public R<Void> deleteDeployment(@RequestParam String id) {
        camundaService.deleteDeployment(id);
        return R.ok();
    }


    @GetMapping(value = "/processDefinitionListByKey")
    public R<List> processDefinitionListByKey(String processDefinitionKey) {
        return R.ok(camundaService.processDefinitionListByKey(processDefinitionKey));
    }

    /**
     * 查看待办任务
     */
    @GetMapping(value = "/todoList")
    public R<Page> todoList(TodoTaskDto todoTaskDto, PageModel page) {
        return R.ok(camundaService.todoList(todoTaskDto, page));
    }

    /**
     * 查看已办任务
     */
    @GetMapping(value = "/todoneList")
    public R<Page> todoneList(TodoneTaskDto todoneTaskDto, PageModel page) {
        return R.ok(camundaService.todoneList(todoneTaskDto, page));
    }

    /**
     * 查看已发任务
     */
    @GetMapping(value = "/applyList")
    public R<Page> applyList(ApplyDto applyDto, PageModel page) {
        return R.ok(camundaService.applyList(applyDto, page));
    }


    @GetMapping(value = "/getProcessModel")
    public R<ProcessModelVo> getProcessModel(String processDefinitionId) {
        ProcessModelVo processModelVo = camundaService.getProcessModel(processDefinitionId);
        return R.ok(processModelVo);
    }

    @GetMapping(value = "/getTaskModel")
    public R<ProcessModelVo> getTaskModel(@RequestParam String taskId) {
        ProcessModelVo processModelVo = camundaService.getTaskModel(taskId);
        return R.ok(processModelVo);
    }

    @GetMapping(value = "/getInstanceModel")
    public R<ProcessModelVo> getInstanceModel(@RequestParam String processInstanceId) {
        ProcessModelVo processModelVo = camundaService.getInstanceModel(processInstanceId);
        return R.ok(processModelVo);
    }

    @GetMapping(value = "/listHistoryRecordByTaskId")
    public R<List<HistoryRecordVo>> listHistoryRecordByTask(@RequestParam String taskId) {
        List<HistoryRecordVo> historyRecordVoList = camundaService.listHistoryRecordByTaskId(taskId);
        return R.ok(historyRecordVoList);
    }

    @GetMapping(value = "/listHistoryRecordByProcessInstanceId")
    public R<List<HistoryRecordVo>> listHistoryRecordByProcessInstanceId(@RequestParam String processInstanceId) {
        List<HistoryRecordVo> historyRecordVoList = camundaService.listHistoryRecordByProcessInstanceId(processInstanceId);
        return R.ok(historyRecordVoList);
    }
}
